//导入vue包
import Vue from 'vue';
//导入vue-router的包 */
import vueRouter from 'vue-router';
Vue.use(vueRouter);
//使用vue-resources
import verResource from 'vue-resource';
Vue.use(verResource);
//导入vuex
import vuex from 'vuex';
Vue.use(vuex);
//导入app.vue
import App from './app.vue';

//导入路由规则定义的组件
import home from './components/Home.vue';
import member from './components/member.vue';
import shopcar from './components/shopcar.vue';
import search from './components/search.vue';
import newsList from './components/news/newslist.vue';
import newsInfo from './components/news/newsInfo.vue';
import photolist from './components/photo/photolist.vue';
import photoInfo from './components/photo/photoInfo.vue';
import goodslist from './components/goods/goodsList.vue';
import goodsInfo from './components/goods/goodsInfo.vue';
import goodsDesc from './components/goods/goodsDesc.vue';
import goodsComment from './components/goods/goodsComment.vue';
//定义路由规则
var router = new vueRouter({
    linkActiveClass:'mui-active', //改变路由激活时的class名称
    routes:[
        {path:'/',redirect: '/home'}, //进入系统默认到home路径下
        {path:'/home',component: home},//首页
        {path:'/member',component: member}, //会员
        {path:'/shopcar',component: shopcar}, //购物车
        {path:'/search',component: search}, //搜索
        {path:'/news/newslist',component:newsList},//新闻列表页面
        {path:'/news/newsInfo/:id',component:newsInfo},//新闻列表详情页面
        {path:'/photo/photolist',component:photolist},//图片列表页面
        {path:'/photo/photoInfo/:id',component:photoInfo},//图片列表详情页面
        {path:'/goods/goodslist',component:goodslist},//商品列表页面
        {path:'/goods/goodsInfo/:id',component:goodsInfo,name:'goodsinfo'},//商品列表详情页面
        {path:'/goods/goodsDesc/:id',component:goodsDesc},//商品图文详情页面
        {path:'/goods/goodsComment/:id',component:goodsComment}//商品评论详情页面
    ]
})

//注册mint-ui
//导入mui的css
import 'mint-ui/lib/style.min.css';
//导入Mint-ui组件对象
import mintui from 'mint-ui';
//在Vue中使用mintui
Vue.use(mintui);


//导入mui的css样式
import '../statics/mui/css/mui.css';
import '../statics/mui/css/icons-extra.css';
//导入app全局样式
import '../statics/css/site.css';

//定义一个全局的时间过滤器
//使用 [momentjs](http://momentjs.cn/ "") 实现日期格式化功能
import moment from 'moment';
Vue.filter('datefmt',function(input,fmtStiong){
    return moment(input).format(fmtStiong);
})

// 使用图片预览组件
import VuePreview from 'vue-preview';
Vue.use(VuePreview);
var car = JSON.parse(window.localStorage.getItem("car"));
const store = new vuex.Store({
    state:{
        list:car || [],
        totalCount:0
    },
    mutations: {
        add(state,obj){
            var flag = 0;
            for(let i=0;i<state.list.length;i++){
                if(obj.id == state.list[i].id){
                    flag = 1;
                    state.list[i].count = parseInt(state.list[i].count)+parseInt(obj.count);
                    break;
                }
            }
            if(flag == 0){
                state.list.push(obj);
            }
            window.localStorage.setItem("car",JSON.stringify(state.list));
        },
        changeStatus(state,obj){
            state.list.some(item=>{
                if(item.id == obj.id){
                    item.selected = obj.selected;
                    return true;
                }
            })
            window.localStorage.setItem("car",JSON.stringify(state.list));
        },
        updateGoodsNumber(state,obj){
            state.list.some(item=>{
                if(item.id == obj.id){
                    item.count = obj.value;
                    return true;
                }
            })
            window.localStorage.setItem("car",JSON.stringify(state.list));
        },
        removeGoods(state,id){
            state.list.some((item,index)=>{
                if(item.id == id){
                    state.car.splice(index,1);
                    return true;
                }
            })
            window.localStorage.setItem("car",JSON.stringify(state.list));
        }
    },
    getters:{
       getCount(state){
            var k=0;
            for(let i=0;i<state.list.length;i++){
                k += parseInt(state.list[i].count);
            }
            state.totalCount = k;
            return k;
       },
       getGoodsCount(state){
            var o = {};
            state.list.forEach(item =>{
                o[item.id] = item.count;
            })
            return o;
       },
       getGoodsStatu(state){
           var o = {};
           state.list.forEach(item =>{
                o[item.id] = item.selected;
            })
            return o;
       },
       getTotalGoods(state){
            var o = {
                number:0,
                price:0
            }
            state.list.forEach(item=>{
                if(item.selected){
                    o.number += parseInt(item.count);
                    o.price += parseInt(item.price*item.count);
                }
            })
            return o;
       }
    }
})
//利用Vue对象进行解析渲染
new Vue({
    el:"#app",
    router:router,
    store:store,
    render:function (createElement) {
        return createElement(App)
    }
})
